﻿using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.auto
{
    public class JDZJ : Information
    {
        List<string> list = new List<string>();
        int num = 0;
        public override bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("user", name);
            pd.Add("pass", pass);
            pd.Add("regjm", vcode);
            pd.Add("login", "登陆");
            pd.Add("action", "login");

            string html = source.Post("http://my.jdzj.com/Chkadmin.asp", pd.Read(), "http://www.jdzj.com/");
            if (html.IndexOf("您好， " + name) != -1)
            {
                num = site.TodayPublicNum;
                return true;
            }
            return false;
        }

        public override InformationResult PostArticle(string title, string content, string vcode)
        {
            title = tek.Get(title);
            //过滤非法关键字
            title = ReplaceIllegalKeyword(title);
            content = ReplaceIllegalKeyword(content);
            InformationResult result = new InformationResult();
            string html = source.Get("http://my.jdzj.com/gycpaddsok20111002.asp", "http://my.jdzj.com/cp.asp");
            ParametersData pd = new ParametersData("gb2312");
            pd.Add("aaa", Regex.Match(html, "<input name=\"aaa\" type=\"hidden\" value=\"([0-9]+)\">").Groups[1].Value);
            pd.Add("bbb", Regex.Match(html, "<input name=\"bbb\" type=\"hidden\" value=\"([0-9]+)\">").Groups[1].Value);
            pd.Add("ccc", Regex.Match(html, "<input name=\"ccc\" type=\"hidden\" value=\"([0-9]+)\">").Groups[1].Value);
            pd.Add("ddd", Regex.Match(html, "<input name=\"ddd\" type=\"hidden\" value=\"([0-9]+)\">").Groups[1].Value);
            pd.Add("cpname", title);
            pd.Add("iweb", "1");
            pd.Add("sortid", site.TypeId[0]);
            pd.Add("sort2id", site.TypeId[1]);
            pd.Add("cpjg", pif.Get("价格"));
            pd.Add("jgdw", "人民币");
            pd.Add("cpjl", pif.Get("单位"));
            pd.Add("cpcd", pif.Get("产品产地"));
            pd.Add("cpsb", pif.Get("产品名称"));
            pd.Add("cpgg", pif.Get("产品描述"));
            pd.Add("cpbh", "");
            pd.Add("cpfhd", pif.Get("产品发货地"));
            pd.Add("fkfs", "3");
            pd.Add("ypxxtwo_id", site.TypeId[2]);
            pd.Add("cxljdzj_pic", UpdateImage());
            pd.Add("cxljdzj_zl", "");
            pd.Add("zdzlm", "");
            pd.Add("cxljdzj_zdzl", "");
            pd.Add("cpjm", "");
            pd.Add("cpsm", content);
            pd.Add("button", "产品资料准确无误请 提交");
            //机电之家发布间隔
            Sleep();
            html = source.Post("http://my.jdzj.com/cxljdzj_cp_add_sveaikmbb.asp", pd.Read(), "http://my.jdzj.com/gycpaddsok20111002.asp");
            if (html.IndexOf("<td height=111 align=center>恭喜您，操作成功") != -1)
            {
                result.Code = 1;
                result.Content = Regex.Match(html, "<a href=\"(http://www.jdzj.com/products/[^\"]+)\" target=\"_blank\">点击查看 </a><br />").Groups[1].Value;
                //机电之家防群发检测
                string aid = Regex.Match(result.Content, "([0-9]+)-1\\.html").Groups[1].Value;
                html = source.Get("http://kf.jdzj.com/kf/index.asp?u=8888", "http://my.jdzj.com/ok1.asp?url=cp.asp");
                source.Get("http://kf.jdzj.com/online.asp?lailu=http://my.jdzj.com/cp_edit.asp?page=&id=" + aid + "&ip=" + Regex.Match(html, "id='ip' type=hidden value='([^']+)'").Groups[1].Value + "&urll=http%3A//my.jdzj.com/ok1.asp%3Furl%3Dcp.asp&yq=1&sjs=" + Regex.Match(html, "id='sjs' type=hidden value='([0-9]+)'").Groups[1].Value + "&kuse=8888", "http://my.jdzj.com/ok1.asp?url=cp.asp");
            }
            else if (html.IndexOf("可能存在的原因：</strong><br>   您的积分不够了，您可以") != -1)
            {
                result.Code = 3;
            }
            else if (Regex.IsMatch(html, "<script>alert\\('([^']+)'\\)"))
            {
                result.Content = Regex.Match(html, "<script>alert\\('([^']+)'\\)").Groups[1].Value;
            }
            else if (html.IndexOf("您的企业信息被关闭，关闭的可能原因为：<br>") != -1)
            {
                result.Code = 3;
                result.Content = "您的企业信息被关闭";
            }
            return result;
        }
        //自动休息时间间隔
        public void Sleep()
        {
            num++;
            int i = num / 100;
            if (i == 0)
            {
                MyFunction.Sleep(15);
            }
            else
            {
                MyFunction.Sleep(i * 70);
            }
        }
        //上传图片
        private string UpdateImage()
        {
            SourceFormData sfd = new SourceFormData("gb2312");
            sfd.WriteFile("filedata", RandomPicture.Get());
            sfd.Write("Submit", "上传");
            string html = source.Post("http://my.jdzj.com/UPfile_SpzsPic.asp", sfd.GetContentType(), sfd.Read(), "http://my.jdzj.com/Upload_SpzsPic.asp");
            return Regex.Match(html, "value='(UserDocument[^']+)';").Groups[1].Value;
        }
        public override Image GetFlushCodeImage()
        {
            return source.GetImage("http://my.jdzj.com/incing/getcode.asp");
        }
        public string ReplaceIllegalKeyword(string str)
        {
            str = str.Replace("淘宝", "");
            str = str.Replace("百度", "");
            return str;
        }
    }
}
